package com.young.common.context;

import com.young.common.core.net.InetTool;
import org.springframework.core.env.Environment;

import java.util.Optional;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:13 PM
 */
public final class ContextEnvironmentTool {
    public ContextEnvironmentTool() {
    }

    public static String preferProfile(final Environment environment) {
        return profile(environment).prefer();
    }

    public static Profile profile(final Environment environment) {
        String source = environment.getProperty("profile");
        if (source == null) {
            source = environment.getProperty("spring.application.prefer-profile");
        }

        if (source == null) {
            String[] profiles = environment.getActiveProfiles();
            if (profiles.length == 0) {
                String hostName = InetTool.getHostName(true);
                if (hostName.contains(".prod.")) {
                    return Profile.PROD;
                }

                if (hostName.contains(".pre.")) {
                    return Profile.PRE;
                }

                if (hostName.contains(".beta.")) {
                    return Profile.BETA;
                }

                if (hostName.contains(".test.")) {
                    return Profile.TEST;
                }

                if (hostName.contains(".dev.")) {
                    return Profile.DEV;
                }

                profiles = environment.getDefaultProfiles();
            }

            source = profiles[profiles.length - 1];
        }

        return Profile.profileOf(source);
    }

    public static String project(final Environment environment) {
        String source = environment.getProperty("project");
        if (source != null) {
            return source;
        } else {
            source = environment.getProperty("PROJECT");
            if (source != null) {
                return source;
            } else {
                source = environment.getProperty("spring.application.project");
                return source != null ? source : environment.getProperty("spring.application.name");
            }
        }
    }

    public static String cloud(final Environment environment) {
        String source = environment.getProperty("cloud");
        if (source != null) {
            return source;
        } else {
            source = environment.getProperty("CLOUD");
            if (source != null) {
                return source;
            } else {
                String hostName = InetTool.getHostName(true);
                if (hostName.contains(".aws.")) {
                    source = Cloud.AWS.code;
                } else if (hostName.contains(".ali.")) {
                    source = Cloud.ALI.code;
                } else if (hostName.contains(".ten.")) {
                    source = Cloud.TEN.code;
                } else {
                    source = Cloud.DEFAULT.code;
                }

                return source;
            }
        }
    }

    public static String group(final Environment environment) {
        String source = environment.getProperty("group");
        return source != null ? source : environment.getProperty("GROUP");
    }

    public static String container(final Environment environment) {
        String source = environment.getProperty("container");
        return source != null ? source : environment.getProperty("CONTAINER");
    }

    public static String httpClientType(final Environment environment) {
        return environment.getProperty("spring.http-client.type", "httpComponents");
    }

    public static Optional<String> env(final Environment environment) {
        String source = environment.getProperty("env");
        if (source == null) {
            source = environment.getProperty("ENV");
        }

        return Optional.ofNullable(source);
    }

    public static Optional<String> propertySelector(final Environment environment) {
        String source = environment.getProperty("property.selector");
        return Optional.ofNullable(source);
    }
}
